#include "StdAfx.h"
#include "RoomManager.h"

#define MAXNUM_USER		4

CRoomManager* CRoomManager::m_Instance = NULL;

CRoomManager::CRoomManager(void)
{

}

CRoomManager::~CRoomManager(void)
{

}

void CRoomManager::initialize(void)
{
	m_mBoard.clear();
}

CRoomManager* CRoomManager::getInstance(void)
{
	return m_Instance;
}

void CRoomManager::createInstance(void)
{
	if( m_Instance == NULL )
	{
		m_Instance = new CRoomManager;
		CRoomManager::getInstance()->initialize();
	}
}

void CRoomManager::destroy(void)
{
	if( m_Instance != NULL)
	{
		delete m_Instance;
	}
}

bool CRoomManager::createRoom(jint roomId)
{
	bool isRoomExit = false;
	if( m_mBoard.find(roomId) != m_mBoard.end() )
	{
		isRoomExit = true;
		cout<<"]![ERROR:createRoom]Already room Exits.."<<endl;
	}

	if( isRoomExit == false )
	{
		//create Room here.
		typedef pair<jint, CBoardManager*> class_pair;
		map<jint, CBoardManager*>::iterator mClass_iter;
		m_mBoard.insert( class_pair(roomId, new CBoardManager));
		mClass_iter = m_mBoard.find(roomId);
		if (mClass_iter!= m_mBoard.end())
		{
			if (mClass_iter->second != NULL)
			{
				mClass_iter->second->initialize();
			}
			else
			{
				cout<<"]![ERROR:createRoom]Allocation Board is failed."<<endl;
			}
		}

		typedef pair<jint, int> cntUser_pair;
		m_mCntUser.insert( cntUser_pair(roomId, 0));

		return true;
	}

	return false;
}

bool CRoomManager::destroyRoom(jint roomId)
{
	bool isRoomExit = false;

	if( m_mBoard.find(roomId) != m_mBoard.end() )
	{
		isRoomExit = true;
	}

	if( isRoomExit == true )
	{
		//delete Room her
		delete (m_mBoard.find(roomId)->second);
		m_mBoard.erase(roomId);

		if( m_mCntUser.find(roomId) != m_mCntUser.end())
		{
			m_mCntUser.erase(roomId);
		}

		return true;
	}

	return false;
}

void CRoomManager::enterUser(jint roomId)
{
	int tempusercnt = 0;

	if( m_mCntUser.find(roomId) != m_mCntUser.end() )
	{
		if( m_mCntUser.find(roomId)->second < MAXNUM_USER)
		{
			(m_mCntUser.find(roomId)->second)++;
			tempusercnt = m_mCntUser.find(roomId)->second;
			cout<<"[USERLOG]Number of user : "<<tempusercnt<<endl;
		}
		else
		{
			//num of user is same with or bigger than MAXNUM_USER.
			cout<<"]*[ERROR:enterUser]num of user is same with or bigger than MAXNUM_USER!."<<endl;
		}

		if( m_mBoard.find(roomId) != m_mBoard.end() )
		{
			m_mBoard.find(roomId)->second->setUserNum(tempusercnt);
		}
	}
}

void CRoomManager::RoomGame(jint roomId, PLAYER whoseturn, int index)
{
	if( m_mBoard.find(roomId) != m_mBoard.end() )
	{
		if(m_mBoard.find(roomId)->second != NULL)
		{
			if( whoseturn <= player4 )
			{
				m_mBoard.find(roomId)->second->gameLogic(whoseturn, index, roomId);
			}
			else
			{
				cout<<"]*[ERROR:RoomGame]PLAYER value is not valid."<<endl; 
			}
		}
		else
		{
			cout<<"]![ERROR:RoomGame]Board is NULL."<<endl;
		}
	}
	else
	{
		cout<<"]![ERROR:RoomGame]There is no board associated with Room."<<endl;
	}
}

void CRoomManager::initializeRoom(jint roomId)
{
	if(m_mBoard.find(roomId) == m_mBoard.end())
	{
		cout<<"]![ERROR:initializeRoom]There is no Board of roomID["<<roomId<<"]!"<<endl;
	}

	if(m_mBoard.find(roomId)->second == NULL)
	{
		cout<<"]![ERROR:initializeRoom]There is no Board allocated and pBoard is NULL"<<endl;
	}
	else
	{
		m_mBoard.find(roomId)->second->initialize();
		m_mCntUser.find(roomId)->second = 0;
	}
	
	
}